package zgame.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zgame.socket.server.ServerConnection;

public class Service {
  public enum TYPE {CURRENT, OTHER};
  public enum FUNCTION {AUTHENTICATE, GAME, CHAT};
  
  private String id;
  private String name;
  private String description = "";
  private FUNCTION function;
  private TYPE type;
  private String validateKey;
  private String url;
  private Map<String, Port> ports = new HashMap<String, Port>();
  private int cocurrentUser;
  private int maxUser;
  private ServerConnection server;
  private Map<String, User> users = new HashMap<String, User>();

  public Service(String id, String name) {
    this.id = id;
    this.name = name;
  }

  public String getName() {
    return name;
  }

  public Service setName(String name) {
    this.name = name;
    return this;
  }
  
  public String getDescription() {
    return description;
  }

  public Service setDescription(String description) {
    this.description = description;
    return this;
  }

  public int getCocurrentUser() {
    return cocurrentUser;
  }

  public Service setCocurrentUser(int cocurrentUser) {
    this.cocurrentUser = cocurrentUser;
    return this;
  }

  public int getMaxUser() {
    return maxUser;
  }

  public Service setMaxUser(int maxUser) {
    this.maxUser = maxUser;
    return this;
  }

  public String getId() {
    return id;
  }

  public Service setUrl(String url) {
    this.url = url;
    return this;
  }

  public String getUrl() {
    return url;
  }

  public void addPort(Port port) {
    ports.put(port.getName(), port);
  }
  
  public Port getPort(String portName) {
    return ports.get(portName);
  }
  
  public List<Port> getPorts() {
    return new ArrayList<Port>(ports.values());
  }
  
  public FUNCTION getFunction() {
    return function;
  }

  public Service setFunction(FUNCTION function) {
    this.function = function;
    return this;
  }

  public TYPE getType() {
    return type;
  }

  public Service setType(TYPE type) {
    this.type = type;
    return this;
  }

  public String getValidateKey() {
    return validateKey;
  }

  public Service setValidateKey(String validateKey) {
    this.validateKey = validateKey;
    return this;
  }

  public ServerConnection getServer() {
    return server;
  }

  public Service setServer(ServerConnection server) {
    this.server = server;
    return this;
  }

  public void onUserJoinIn(User user) {
    users.put(user.getUsername(), user);
  }

  public void onUserGetOut(String username) {
    users.remove(username);
  }

  public Map<String, User> getUserMap() {
    return users;
  }

  public String toString() {
    StringBuffer buffer = new StringBuffer("Service [");
    buffer.append("id=\"" + id + "\"");
    buffer.append(", name=\"" + name + "\"");
    buffer.append(", description=\"" + description + "\"");
    buffer.append(", function=\"" + function + "\"");
    buffer.append(", validateKey=\"" + validateKey + "\"");
    buffer.append(", type=\"" + type + "\"");
    buffer.append(", url=\"" + url + "\"");
    
    for (Port port : ports.values()) {
      buffer.append(", port \"" + port.getName() + "\"" + " : " + port.getValue() + " : \"" + port.getDescription() + "\"");
    }
    buffer.append("]");
    return buffer.toString();
  }
}
